<style scoped lang="scss">

</style>

<template>
  <div>

    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="仓库名称 : ">
        <el-input v-model="queryParams.repositoryName" placeholder="仓库名称"></el-input>
      </el-form-item>
      <el-form-item label="仓库代号 : ">
        <el-input v-model="queryParams.repositoryCode" placeholder="仓库代号"></el-input>
      </el-form-item>
      <el-form-item label="启动时间范围 : ">
        <el-date-picker v-model="timeSlice" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仓库状态 : ">
        <el-radio v-model="queryParams.repositoryState" :label=0>停用</el-radio>
        <el-radio v-model="queryParams.repositoryState" :label=1>启用</el-radio>
        <el-radio v-model="queryParams.repositoryState" :label=-1 default>全部查看</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleAdd" plain icon="el-icon-plus" size="mini">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" @click="handleDelete()" plain icon="el-icon-delete" size="mini">删除</el-button>
      </el-col>
    </el-row>
    <br>
    <el-table :data="tableData"
              v-loading="loading"
              stripe border
              @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column prop="repositoryId" label="仓库主键">
      </el-table-column>
      <el-table-column prop="repositoryName" label="仓库名称">
      </el-table-column>
      <el-table-column prop="repositoryCode" label="仓库代号">
      </el-table-column>
      <el-table-column prop="repositoryPhone" label="联系电话">
      </el-table-column>
      <el-table-column prop="repositoryState" label="仓库状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.repositoryState==0" type="danger">停用</el-tag>
          <el-tag v-else-if="scope.row.repositoryState==1" type="success">正常</el-tag>
          <el-tag v-else type="info">未添加</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="repositoryAddress" label="仓库地址">
      </el-table-column>
      <el-table-column prop="repositoryStartDate" label="启动时间" :formatter="dateFormat">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row.repositoryId)" plain icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row.repositoryId)" plain icon="el-icon-delete"
                     size="mini">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" append-to-body>

      <el-form ref="dialogForm" :model="dialogForm" :rules="dialogRules" lable-width="80px">

        <el-form-item prop="repositoryName" label="仓库名称 : ">
          <el-input v-model="dialogForm.repositoryName" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryCode" label="仓库代号 : ">
          <el-input v-model="dialogForm.repositoryCode" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryPhone" label="联系电话 : ">
          <el-input v-model="dialogForm.repositoryPhone" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryState" label="仓库状态 : ">
          <el-radio-group v-model="dialogForm.repositoryState">
            <el-radio :label=0>停用</el-radio>
            <el-radio :label=1>启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="repositoryAddress" label="仓库地址 : ">
          <el-input v-model="dialogForm.repositoryAddress" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryStartDate" label="启动时间 : ">
          <el-date-picker type="date" v-model="dialogForm.repositoryStartDate" placeholder="开始日期">
          </el-date-picker>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import {editPage, dialogSubmit, getBasePage, deleteBase} from '@/api/RepositoryInfo'

export default {
  name: "repositoryInfo",
  created() {
    this.getList()
  },
  data() {
    return {
      circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",

      tableData: [],
      selectedIds: [],

      title: "",
      total: 0,
      timeSlice: [],

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repositoryName: null,
        repositoryCode: null,
        repositoryStartDateFrom: null,
        repositoryStartDateTo: null,
        repositoryState: -1
      },
      dialogForm: {
        repositoryName: null,
        repositoryCode: null,
        repositoryPhone: null,
        repositoryStartDate: null,
        repositoryState: null,
        repositoryAddress: null
      },
      dialogRules: {
        repositoryName: [
          {required: true, message: '请输入仓库名称', trigger: 'blur'},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ],
        repositoryCode: [
          {required: true, message: '请输入仓库代号', trigger: 'blur'}
        ],
        repositoryAddress: [
          {min: 0, max: 500, message: '长度在 500 个字符以内', trigger: 'blur'}
        ]
      },

      loading: false,
      dialogVisible: false
    }
  },
  methods: {
    query() {
      this.pageNum = 1
      this.queryParams.repositoryStartDateFrom = this.timeSlice ? this.timeSlice[0] : null
      this.queryParams.repositoryStartDateTo = this.timeSlice ? this.timeSlice[1] : null
      console.log(this.queryParams)
      this.getList()
    },
    resetDialogForm() {
      this.dialogForm = {
        repositoryName: null,
        repositoryCode: null,
        repositoryPhone: null,
        repositoryStartDate: null,
        repositoryState: 1,
        repositoryAddress: null
      }
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.queryParams.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.queryParams.pageNum = pageNum
      this.getList()
    },
    dateFormat(row, column, cellValue, index) {
      return cellValue ? cellValue.substring(0, 10) : cellValue
    },
    getList() {
      this.loading = true
      getBasePage(this.queryParams).then(res => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    onSubmit() {
      console.log('submit!')
      console.log(this.dialogForm)
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {

          this.loading = true

          dialogSubmit(this.dialogForm, this.title).then(res => {
            if (res.code != 200) {
              this.$message({
                message: '添加失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.dialogVisible = false
            this.loading = false
            this.getList()

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    handleAdd() {
      this.resetDialogForm()
      this.title = "添加"
      this.dialogVisible = true
    },
    handleEdit(id) {
      this.loading = true
      editPage(id).then(res => {
        console.log(res);
        this.dialogForm = res.data
        this.title = "修改"
        this.loading = false
        this.dialogVisible = true
      })
    },
    handleDelete(id) {
      let ids = id || this.selectedIds.join(",")
      if (!ids) {
        this.$message({
          message: '请选择要删除的项',
          type: 'warning'
        });
        return
      }

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBase(ids)
            .then(res => {
              console.log(res);
              if (res.code != 200) {
                this.$message({
                  message: '删除失败',
                  type: 'warning'
                });
                return
              }
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getList()
            })
            .catch(err => {
              this.$message({
                message: '删除异常',
                type: 'error'
              });
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    handleSelectionChange(val) {
      this.selectedIds = val.map(item => item.repositoryId)
      console.log(this.selectedIds)
    }
  }
}
</script>